<?php
require_once("./define/define_table.php");
require_once("./class/Frequence.php");
require_once("./class/Association.php");
require_once("./class/Lieu.php");
define("ADD_EVENEMENT_FORM", "Formulaire d'ajout d'un evenement");
define("MODIFY_EVENEMENT_FORM", "Formulaire de modification d'un evenement");

class Evenement{
	
	private $id_evenement;
	private $id_association;
	private $id_lieu;
	private $id_frequence;
	private $nom;
	private $date_creation;
	private $cout;
	private $tarif_exte;
	private $tarif_adherent;
	private $description;
	private $date_debut;
	private $date_fin;
	private $nombre_place;
	
	public function Evenement(){
	       
	}
	
	public function getIdEvenement(){
		return $this->id_evenement;
	}
	
	public function getIdAssociation(){
		return $this->id_association;
	}
	
	public function getIdLieu(){
		return $this->id_lieu;
	}
	
	public function getIdFrequence(){
	    return $this->id_frequence;
	}
	
	public function getNom(){
		return $this->nom;
	}
	
	public function getDateCreation(){
		return $this->date_creation;
	}
	
	public function getCout(){
		return $this->cout;
	}
	
	public function getTarifExte(){
		return $this->tarif_exte;
	}
	
	public function getTarifAdherent(){
		return $this->tarif_adherent;
	}
	
	public function getDescription(){
		return $this->description;
	}
	
	public function getDateDebut(){
		return $this->date_debut;
	}
	
	public function getDateFin(){
		return $this->date_fin;
	}
	
	public function getNombrePlace(){
		return $this->nombre_place;
	}
	
	public function setIdEvenement($id){
	    $this->id_evenement = $id;
	}
	
	public function setIdAssociation($id){
	    $this->id_association = $id;
	}
	
	public function setIdLieu($id){
	    $this->id_lieu = $id;
	}
	
	public function setIdFrequence($id){
	    $this->id_frequence = $id;
	}
	
	public function setNom($nom){
	    $this->nom = $nom;
	}
	
	public function setDateCreation($date){
		 $this->date_creation = $date;
	}

	public function setCout($cout){
		$this->cout = $cout;
	}

	public function setTarifExte($tarif){
	 $this->tarif_exte = $tarif;
	}

	public function setTarifAdherent($tarif){
	 $this->tarif_adherent = $tarif;
	}

	public function setDescription($description){
	 $this->description = $description;
	}

	public function setDateDebut($date){
	 $this->date_debut = $date;
	}

	public function setDateFin($date){
	 $this->date_fin = $date;
	}

	public function setNombrePlace($nombre_place){
	 $this->nombre_place = $nombre_place;
	}
	
	public function getAllEvenement(){
	    global $db;
		try{
			$sql = "SELECT * FROM ".EVENEMENT_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Evenement");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getEvenement($id){
	    global $db;
	    try{
	        $sql = "SELECT * FROM ".EVENEMENT_TABLE." WHERE id_evenement = :id_evenement";
	        $sth = $db->prepare($sql);
	        $sth->bindValue(":id_evenement", $id, PDO::PARAM_INT);
	        $sth->execute();
	        return $sth->fetchObject("Evenement");
	    }catch(PDOException $e){
	        echo $e.getMessage();
	    }
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_evenement == null){
				$sql = "INSERT INTO ".EVENEMENT_TABLE.
							 " VALUES(null, :id_association, :id_lieu, :id_frequence, :nom, :date_creation,  :cout, :tarif_exte, :tarif_adherent,
							 :description, :date_debut, :date_fin, :nombre_place)";
				$sth = $db->prepare($sql);
	 			$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
	 			$sth->bindValue(":id_lieu", $this->id_lieu, PDO::PARAM_INT);
	 			$sth->bindValue(":id_frequence", $this->id_frequence, PDO::PARAM_INT);
	 			$sth->bindValue(":date_creation", $this->date_creation);
	 			$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
	 			$sth->bindValue(":cout", $this->cout);
				$sth->bindValue(":tarif_exte", $this->tarif_exte);
				$sth->bindValue(":tarif_adherent", $this->tarif_adherent);
				$sth->bindValue(":description", $this->description, PDO::PARAM_STR);
				$sth->bindValue(":date_debut", $this->date_debut);
	 			$sth->bindValue(":date_fin",	$this->date_fin);
				$sth->bindValue(":nombre_place", $this->nombre_place, PDO::PARAM_INT);
			}
			else{
				$sql = "UPDATE ".EVENEMENT_TABLE." SET id_association = :id_association, id_lieu = :id_lieu, id_frequence = :id_frequence,
					date_creation = :date_creation, nom = :nom, cout = :cout, tarif_exte = :tarif_exte, 
					tarif_adherent = :tarif_adherent,	description = :description, date_debut = :date_debut, 
					date_fin = :date_fin, nombre_place = :nombre_place WHERE id_evenement= :id_evenement";
				$sth = $db->prepare($sql);
	 			$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
	 			$sth->bindValue(":id_lieu", $this->id_lieu, PDO::PARAM_INT);
	 			$sth->bindValue(":id_frequence", $this->id_frequence, PDO::PARAM_INT);
	 			$sth->bindValue(":date_creation", $this->date_creation);
	 			$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
	 			$sth->bindValue(":cout", $this->cout);
				$sth->bindValue(":tarif_exte", $this->tarif_exte);
				$sth->bindValue(":tarif_adherent", $this->tarif_adherent);
				$sth->bindValue(":description", $this->description, PDO::PARAM_STR);
				$sth->bindValue(":date_debut", $this->date_debut);
	 			$sth->bindValue(":date_fin",	$this->date_fin);
				$sth->bindValue(":nombre_place", $this->nombre_place, PDO::PARAM_INT);
				$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_EVENEMENT_FORM .
				'<form class="ajax_form" id="add_evenement_form" action="?module=evenement&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._NOM_EVENEMENT_.'</td>' .
				'			<td><input type="text" name="nom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ASSOCIATION_.'</td>' .
				'			<td>'.Association::generateSelect().'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._LIEU_.'</td>' .
				'			<td>'.Lieu::generateSelect().'</td>' .
				'		</tr>' .
		'				<tr>' .
								'			<td>'._FREQUENCE_.'</td>' .
								'			<td>'.Frequence::generateSelect().'</td>' .
								'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_CREATION_.'</td>' .
				'			<td><input type="text" name="date_creation" class="datepicker" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._COUT_EVENEMENT_.'</td>' .
				'			<td><input type="text" name="cout" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._TARIF_EXTERIEUR_.'</td>' .
				'			<td><input type="text" name="tarif_exte" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._TARIF_ADHERENT_.'</td>' .
				'			<td><input type="text" name="tarif_adherent" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DESCRIPTION_.'</td>' .
				'			<td><input type="text" name="description" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_DEBUT_.'</td>' .
				'			<td><input type="text" name="date_debut" class="datepicker" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_FIN_.'</td>' .
				'			<td><input type="text" name="date_fin" class="datepicker" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOMBRE_PLACE_.'</td>' .
				'			<td><input type="text" name="nombre_place" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_EVENEMENT_FORM .
				'<form class="ajax_form" id="modify_evenement_form" action="?module=evenement&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_EVENEMENT_.'</td>' .
				'			<td><input readonly type="text" name="id_evenement" value="'.$this->id_evenement.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOM_EVENEMENT_.'</td>' .
				'			<td><input type="text" name="nom" value="'.$this->nom.'" /></td>' .
				'		<tr>' .
				'			<td>'._ASSOCIATION_.'</td>' .
				'			<td>'.Association::generateSelect($this->id_association).'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._LIEU_.'</td>' .
				'			<td>'.Lieu::generateSelect($this->id_lieu).'</td>' .
				'		</tr>' .
		        '		<tr>' .
						'			<td>'._FREQUENCE_.'</td>' .
						'			<td>'.Frequence::generateSelect($this->id_frequence).'</td>' .
						'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_CREATION_.'</td>' .
				'			<td><input type="text" name="date_creation" value = "'.$this->date_creation.'" class="datepicker" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._COUT_EVENEMENT_.'</td>' .
				'			<td><input type="text" name="cout" value = "'.$this->cout.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._TARIF_EXTERIEUR_.'</td>' .
				'			<td><input type="text" name="tarif_exte" value = "'.$this->tarif_exte.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._TARIF_ADHERENT_.'</td>' .
				'			<td><input type="text" name="tarif_adherent" value = "'.$this->tarif_adherent.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DESCRIPTION_.'</td>' .
				'			<td><input type="text" name="description" value = "'.$this->description.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_DEBUT_.'</td>' .
				'			<td><input type="text" name="date_debut" value = "'.$this->date_debut.'" class="datepicker"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DATE_FIN_.'</td>' . 
				'			<td><input type="text" name="date_fin" value = "'.$this->date_fin.'" class="datepicker"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOMBRE_PLACE_.'</td>' .
				'			<td><input type="text" name="nombre_place" value ="'.$this->nombre_place.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}

	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".EVENEMENT_TABLE." WHERE id_evenement = :id_evenement";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}

    public function generateSelect($id = null){
        $array_evenement= self::getAllEvenement();
        $html = '<select name="id_evenement">';
        foreach($array_evenement as $e){
            $selected = ($e->getIdEvenement() == $id) ?  'selected="selected"' : '';
            $html .= '<option value="'.$e->getIdEvenement().'" '.$selected.'>'.$e->getNom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }
}
?>
